#include "stdafx.h"
#include "Thread.h"

namespace Core
{

	Thread::Thread()
	{
		pthread_attr_init(&m_Attribute);
		pthread_attr_setdetachstate(&m_Attribute, PTHREAD_CREATE_JOINABLE);
	}

	Thread::~Thread()
	{
		pthread_attr_destroy(&m_Attribute);
		pthread_exit(NULL);
	}

	void* Thread::Entry(void* p_PtrThis)
	{
		Thread* t_Thread = reinterpret_cast<Thread*>(p_PtrThis);
		if(t_Thread)
		{
			t_Thread->Run();
		}
		return NULL;
	}

	void Thread::Run()
	{
		Execute();
	}

	void Thread::Start()
	{
		pthread_create(&m_ThreadDescriptor, &m_Attribute, &Entry, this);
	}

	void Thread::Exit()
	{
		pthread_exit(&m_ThreadDescriptor);
	}

	int Thread::Join()
	{
		int t_Status = pthread_join(m_ThreadDescriptor, NULL);	
		return t_Status;
	}

}
